<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<title>Closure Unit Tests - goog.testing.DeferredTestCase Asyncronous Tests</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.async.Deferred');
    goog.require('goog.testing.DeferredTestCase');
    goog.require('goog.testing.TestRunner');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.recordFunction');
  </script>
</head>
<body>
  <script type="text/javascript">
    var deferredTestCase =
        goog.testing.DeferredTestCase.createAndInstall(document.title);
    var testTestCase;
    var runner;

    // Optionally, set a longer-than-usual step timeout.
    deferredTestCase.stepTimeout = 15 * 1000; // 15 seconds

    // This is the sample code in deferredtestcase.js
    function testDeferredCallbacks() {
      var callbackTime = goog.now();
      var callbacks = new goog.async.Deferred();
      deferredTestCase.addWaitForAsync('Waiting for 1st callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });
      deferredTestCase.addWaitForAsync('Waiting for 2nd callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });
      deferredTestCase.addWaitForAsync('Waiting for last callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });

      deferredTestCase.waitForDeferred(callbacks);
    }

    function createDeferredTestCase(d) {
      testTestCase = new goog.testing.DeferredTestCase('Foobar TestCase');
      testTestCase.add(new goog.testing.TestCase.Test(
        'Foobar Test',
        function() {
          this.waitForDeferred(d);
        },
        testTestCase));

      var testCompleteCallback = new goog.async.Deferred();
      testTestCase.setCompletedCallback(
          function() {
            testCompleteCallback.callback(true);
          });

      // We're not going to use the runner to run the test, but we attach one
      // here anyway because without a runner TestCase throws an exception in
      // finalize().
      var runner = new goog.testing.TestRunner();
      runner.initialize(testTestCase);

      return testCompleteCallback;
    }

    function testDeferredWait() {
      var d = new goog.async.Deferred();
      deferredTestCase.addWaitForAsync('Foobar', d);
      d.addCallback(function() {
        return goog.async.Deferred.succeed(true);
      });
      deferredTestCase.waitForDeferred(d);
    }

    function testNonAsync() {
      assertTrue(true);
    }

    function testPassWithTestRunner() {
      var d = new goog.async.Deferred();
      d.addCallback(function() {
        return goog.async.Deferred.succeed(true);
      });

      var testCompleteDeferred = createDeferredTestCase(d);
      testTestCase.execute();

      var deferredCallbackOnPass = new goog.async.Deferred();
      deferredCallbackOnPass.addCallback(function() {
        return testCompleteDeferred;
      });
      deferredCallbackOnPass.addCallback(function() {
        assertTrue('Test case should have succeded.', testTestCase.isSuccess());
      });

      deferredTestCase.waitForDeferred(deferredCallbackOnPass);
    }

    function testFailWithTestRunner() {
      var d = new goog.async.Deferred();
      d.addCallback(function() {
        return goog.async.Deferred.fail(true);
      });

      var testCompleteDeferred = createDeferredTestCase(d);

      // Mock doAsyncError to instead let the test completes successfully,
      // but record the failure. The test works as is because the failing
      // deferred is not actually asynchronous.
      var mockDoAsyncError = goog.testing.recordFunction(function() {
        testTestCase.continueTesting();
      });
      testTestCase.doAsyncError = mockDoAsyncError;

      testTestCase.execute();
      assertEquals(1, mockDoAsyncError.getCallCount());
    }

  </script>
</body>
</html>
